using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerControl : IPlayer
{
 
    bool Cast = true;
    float speed = 0f;
    protected float Attacktime = 1.5f;
    bool Attacking = false;
    private void Start()
    {
        GameFacade.Instance.characterSystem.SetPlayer(this);
        Init();
    }
  
    public override void Attack(int id)
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, 2);
        switch (id)
        {
            case 1:
                LoadClip("Attack1");
                foreach (Collider item in colliders)
                {
                    if(item.tag=="Enemy")
                    {
                        
                        item.GetComponent<IEnemy>().GetHit(50);
                    }
                }
                break;
            case 2:
                LoadClip("Attack2");
                foreach (Collider item in colliders)
                {
                    if (item.tag == "Enemy")
                    {
                        
                        item.GetComponent<IEnemy>().GetHit(80);
                    }
                }
                break;
            case 3:
                LoadClip("Attack3");
                foreach (Collider item in colliders)
                {
                    if (item.tag == "Enemy")
                    {
                        item.GetComponent<IEnemy>().GetHit(100);
                    }
                }
                break;
        }
    }

    public override void GetHit(int damage)
    {
        mAnimator.SetTrigger("GetHit");
      
        MaxHP -= damage;
        audioSource.loop = false;
        LoadClip("GetHit");
        mAnimator.SetBool("Run", false);
        mAnimator.SetBool("Walk", false);
    }

    public override void InAttack()
    {
        audioSource.loop = false;
       
        mAnimator.SetBool("Attack", true);
    }

    public override void InChase()
    {
        audioSource.loop = true;
        LoadClip("PlayerRun");
    }

    public override void InIdle()
    {
        audioSource.Stop();
    }

    public override void LeaveAttack()
    {
        
    }

    public override void LeaveChase()
    {
       
    }

    public override void LeaveIdle()
    {
      
    }

    public override void UpdateAttack()
    {
        AttackanimationSet();
       
        //Camera.main.transform.position = new Vector3(transform.position.x + 4.7f, transform.position.y + 4f, transform.position.z -1.4f);
    }

    public override void UpdateChase()
    {
        //Camera.main.transform.position = new Vector3(transform.position.x + 4.7f, transform.position.y + 4f, transform.position.z - 1.4f);
        AttackanimationSet();
    }

    public override void UpdateIdle()
    {
        AttackanimationSet();
        //Camera.main.transform.position = new Vector3(transform.position.x + 4.7f, transform.position.y + 4f, transform.position.z - 1.4f);
    }
    void LoadClip(string name)
    {
        audioSource.Stop();
        clip = GameFacade.Instance.assetsFactory.LoadAudioclip(name);
        audioSource.clip = clip;
        audioSource.Play();
    }
    protected void Move()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 Direction = new Vector3(-v, 0, h);
        if (Direction != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
        {
            mSystem.PerformTransition(e_Playerstate.Chase);
            speed = 0.5f;
            transform.rotation = Quaternion.LookRotation(Direction);
            transform.Translate(Direction * 0.02f * speed, Space.World);
            audioSource.loop = true;
            clip = GameFacade.Instance.assetsFactory.LoadAudioclip("PlayerWalk");
                if(audioSource.clip!=clip)
            {
                LoadClip("PlayerWalk");
            }
        }
        if (Input.GetKey(KeyCode.LeftShift) && Direction != Vector3.zero)
        {
            audioSource.loop = true;
            mSystem.PerformTransition(e_Playerstate.Chase);
            speed = 1f;
            transform.rotation = Quaternion.LookRotation(Direction);
            transform.Translate(Direction * 0.02f * speed, Space.World);
            clip = GameFacade.Instance.assetsFactory.LoadAudioclip("PlayerRun");
            if (audioSource.clip != clip)
            {
                LoadClip("PlayerRun");
            }
        }
        if (Direction == Vector3.zero)
        {
            mSystem.PerformTransition(e_Playerstate.Idle);
            speed = 0;
        }
        switch (speed)
        {
            case 0.5f:
                mAnimator.SetBool("Walk", true);
                
                break;
            case 1f:
                mAnimator.SetBool("Walk", true);
                mAnimator.SetBool("Run", true);
               
                break;
            case 0f:
                mAnimator.SetBool("Run", false);
                mAnimator.SetBool("Walk", false);
                break;
        }
      
      
        
    }
    void AttackanimationSet()
    {
      
        if (Input.GetMouseButtonDown(0))
        {
            mSystem.PerformTransition(e_Playerstate.Attack);
            Attacking = true;
            Attacktime = 1.5f;
        }
        bool isAnimationPlaying = mAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.95f;
        if (Attacking)
        {
            Attacktime -= 0.02f;
            if(Attacktime<=0&& !isAnimationPlaying)
            {
                mAnimator.SetBool("Attack", false);
                mSystem.PerformTransition(e_Playerstate.Idle);
                Attacking = false;
            }
        }
        if(mSystem.CurrentState.StateId== e_PlayerstateId.Idle|| mSystem.CurrentState.StateId == e_PlayerstateId.Chase)
        {
            if(Cast)
            {
               
                Move();
            }
           
        }
    }
    private void SetMove()
    {
        Cast = true;
    }
    private void CantMove()
    {
        Cast = false;
    }
    private void CounterattackA()
    {
       
        defendsucess = false;
        Collider[] colliders = Physics.OverlapSphere(transform.position, 2);
         foreach (Collider item in colliders)
           {
                if (item.tag == "Enemy")
                    {
                        item.GetComponent<IEnemy>().GetHit(200);
                    }
                }
    }
    private void CounterattackB()
    {
       
        defendsucess = false;
        Collider[] colliders = Physics.OverlapSphere(transform.position, 2);
        foreach (Collider item in colliders)
        {
            if (item.tag == "Enemy")
            {
                item.GetComponent<IEnemy>().GetHit(200);
            }
        }
    }
    private void CounterattackC(int id)
    {
        
        defendsucess = false;
        Collider[] colliders = Physics.OverlapSphere(transform.position, 2);
        foreach (Collider item in colliders)
        {
            if (item.tag == "Enemy")
            {
                switch (id)
                {
                    case 1: item.GetComponent<IEnemy>().GetHit(100);break;
                    case 2: item.GetComponent<IEnemy>().GetHit(150);break;
                }
               
            }
        }
    }
}
